package com.lagou.homework;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


/*
 * @className NumSortMapper
 * @description 利用mr自带的排序能力,经过shuffle,然后在reduce输出数据
 * @author admin
 * @date 2022/5/15 22:41
 * */
// 第一对kv:LongWritable文本偏移量,Text: 一行文本
// 第二对kv:IntWritable->行值,IntWritable->数量1
public class NumSortMapper extends Mapper<LongWritable, Text, IntWritable,IntWritable> {
    private IntWritable number = new IntWritable(); // 行值
    private IntWritable count = new IntWritable(1); // 数量1
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 1.将得到一行文本数据转为字符串
        String line = value.toString();
        // 2.将字符串转为int并赋值给number
        number.set(Integer.parseInt(line));
        // 3.输出kv
        context.write(number,count);
    }
}
